<?php
class Cart_Model_Transaction extends Ext_Db_Table
{
	protected $_name = "pg_transaction";
    protected $_primary = 'id_transaction';
		
	public function saveTransaction( $transaction )
	{
        $writer = new Zend_Log_Writer_Stream(
                          Zend_Registry::get('config')->webpay->log->transaction
                      );
        $log = new Zend_Log( $writer );
        $log->info( "Grabando Transaccion" );

        $data = array(
			'id_transaction' =>  $transaction['TBK_ORDEN_COMPRA'],
            'user_id' =>  $transaction['USER_ID'],
            'account_id' => $transaction['ACCOUNT_ID'],
            'payment_method_id' => $transaction['PAYMENT_METHOD_ID'],
			'object' => $transaction['OBJECT'],
			'modification_date' => $transaction['MODIFICATION_DATE'],
			'amount' => $transaction['AMOUNT'],
			'status' => $transaction['STATUS']
		);  
	
		$row = $this->find( $transaction['TBK_ORDEN_COMPRA'] )->current();

        if ( count( $row ) == 0 ) {
			$row = $this->createRow();
		}
		
		//Guardamos el detalle en la base
		$row->setFromArray( $data );
		$row->save();

        $log->info("Grabando informacion general de la transaccion trans_id ".$data['id_transaction']
        	." account_id ".$data['account_id']
        );
	}

	public function getTransaction( $transactionId )
    {
        $rowset = $this->fetchAll(
            $this->select()
            ->where( 'id_transaction = ?', $transactionId )
        );
        if( count( $rowset ) ) {
            return $rowset;
        }
        return array();
    }

}
